package com.kongxx.wicketstudy.tutorial.user.web;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.kongxx.wicketstudy.tutorial.user.model.User;
import com.kongxx.wicketstudy.tutorial.user.service.UserService;
import com.kongxx.wicketstudy.tutorial.user.service.impl.UserServiceImpl;

public class UserList extends WebPage {

	public UserList() {
		final UserService userService = new UserServiceImpl();
		final List users = userService.list();

		ISortableDataProvider provider = new SortableDataProvider() {
			public int size() {
				return users.size();
			}

			public IModel model(Object object) {
				User user = (User) object;
				return new Model((Serializable) user);
			}

			public Iterator iterator(int first, int count) {
				SortParam sortParam = getSort();
				String sort = "id";
				String order = "asc";
				if (sortParam != null) {
					sort = sortParam.getProperty();
					order = sortParam.isAscending() ? "asc" : "desc";
				}
				return userService.list(first, count, sort, order).iterator();
			}
		};
		IColumn[] columns = {
				new PropertyColumn(new Model("ID"), "id", "id"),
				new PropertyColumn(new Model("Username"), "username",
						"username"),
				new AbstractColumn(new Model("Username")) {
					public void populateItem(Item cellItem, String componentId,
							IModel rowModel) {
						cellItem.add(new UsernamePanel(componentId, rowModel));
					}
				}, new PropertyColumn(new Model("Password"), "password"),
				new PropertyColumn(new Model("Description"), "description"),
				new AbstractColumn(new Model("Delete")) {
					public void populateItem(Item cellItem, String componentId,
							IModel rowModel) {
						String entryId = ((User) rowModel.getObject()).getId();
						cellItem.add(new DeleteUserPanel(componentId, entryId));
					}
				} 
			};
		DefaultDataTable dataTable = new DefaultDataTable("users", columns,
				provider, 3);
		dataTable.addBottomToolbar(new NavigationToolbar(dataTable));

		add(dataTable);
	}

	// public UserList() {
	// final UserService userService = new UserServiceImpl();
	// final List users = userService.list();
	// ListView listView = new ListView("users", users) {
	// protected void populateItem(ListItem item) {
	// final User user = (User) item.getModelObject();
	//
	// // method1
	// // item.add(new Label("id", user.getId()));
	// // item.add(new Label("username", user.getUsername()));
	// // item.add(new Label("password", user.getPassword()));
	// // item.add(new Label("description", user.getDescription()));
	//
	// // method2
	// item.setModel(new CompoundPropertyModel(user));
	// item.add(new AttributeModifier("class", true, new Model(item
	// .getIndex() % 2 == 0 ? "even" : "odd")));
	//
	// item.add(new Label("id"));
	// Link showUserLink = new Link("viewUser") {
	// public void onClick() {
	// UserView userView = new UserView(user);
	// setResponsePage(userView);
	// }
	// };
	// showUserLink.add(new Label("username"));
	// item.add(showUserLink);
	//
	// item.add(new Label("password"));
	// item.add(new Label("description"));
	// }
	// };
	// add(listView);
	// }
}
